package com.zhenhunfan.io.model.bio.demo02;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * <p>Title: BioServerThreadPool</p>
 * <p>Description: </p>
 *
 * @author zhenhunfan
 * @version 1.0.0
 * @date 2021/3/31
 */
public class BioServerThread {
    public static void main(String[] args) {
        int port = 8080;
        ServerSocket serverSocket = null;//服务端
        Socket socket = null;

        try{
            serverSocket = new ServerSocket(port);

            while (true){
                socket = serverSocket.accept();//拿到socket,阻塞方法
                //连接量大的时候会拖垮CPU
                new Thread(new SocketHandler(socket)).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
